package com.ly.jdk8.stream;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import	java.util.stream.Collectors;

/**
 * @Classname EndStream
 * @Description
 * 终止操作
 * 1.txt.查找与匹配
 * allMatch  是否匹配所以元素
 * anyMatch  至少一个元素匹配
 * noneMatch 是否不匹配所有元素
 * findFirst 找到一个
 * findAny   找到任意一个
 * count 返回元素的总个数
 * max 返回元素中的最大值
 * min 返回元素中的最小值
 *
 * 2.归约 reduce
 *  对每一个元素进行关联操作并返回结果
 *
 * 3.收集
 * Collection 返回收集的结果
 * Collectors.toSet() 返回对应的集合
 * Collectors.toCollection  返回传入的集合
 * Collectors.averagingDouble 返回对应类型的平均值
 * Collectors.summingDouble 返回对应类型的总和
 * Collectors.maxBy  需要comparator接口，返回最大值
 * Collectors.minBy  需要comparator接口，返回最小值
 * Collectors.groupingBy  分组
 * Collectors.partitioningBy 分区
 * @Date 2020/9/27 19:47
 * @Author 冷心影翼
 */
public class EndStream {
	private static List<User> u = new ArrayList<>();

	public static void main(String[] args) {
		u.add(new User(18, "A", 0));
		u.add(new User(20, "B", 1));
		u.add(new User(18, "C", 0));
		u.add(new User(16, "D", 1));
		u.add(new User(28, "E", 0));
		u.add(new User(25, "F", 1));
//		find();
//		reduce();
		collection();
	}

	public static void find() {
		System.out.println(u.stream().allMatch(u -> u.getAge() > 20));
		System.out.println(u.stream().anyMatch(u -> u.getAge() > 20));
		System.out.println(u.stream().noneMatch(u -> u.getAge() > 30));
		System.out.println(u.stream().sorted(Comparator.comparingInt(it-> -it.getAge())).findFirst());
		//Stream 串行的方式处理流满足findAny的元素就会直接返回，和findFirst有一定相似度，结果唯一
		//parallelStream 并行的方式处理流,某一线程找到满足的元素就会返回，结果不唯一
		System.out.println(u.stream().filter(it->it.getAge()<25).findAny());
		System.out.println(u.parallelStream().filter(it->it.getAge()<25).findAny());
		System.out.println(u.parallelStream().max(Comparator.comparingInt(User::getAge)));
		System.out.println(u.parallelStream().min(Comparator.comparingInt(User::getAge)));
		System.out.println(u.parallelStream().count());
	}

	public static void reduce() {
		System.out.println(Math.round((double)u.stream()
			.map(User::getAge)
			.reduce(0, (age1, age2) -> age1 + age2)
			/u.size()));
		System.out.println(Math.round((double)u.stream()
			.map(User::getAge)
			.reduce(0, Integer::sum)
			/u.size()));
	}

	public static void collection() {
		Set<User> collect = u.stream().filter(it -> it.getSex() == 1).collect(Collectors.toSet());
		CopyOnWriteArrayList<User> collect1 = u.stream().filter(it -> it.getSex() == 1).collect(Collectors.toCollection(CopyOnWriteArrayList::new));
		System.out.println(collect);
		System.out.println(collect1);
		ConcurrentMap<String, Integer> collect3 = u.stream().collect(Collectors.toConcurrentMap(User::getName, User::getAge));
		System.out.println(u.stream().collect(Collectors.averagingDouble(User::getAge)));
		System.out.println(u.stream().collect(Collectors.summingLong(User::getAge)));
		System.out.println(u.stream().collect(Collectors.maxBy(Comparator.comparingInt(User::getAge))));
		System.out.println(u.stream().collect(Collectors.groupingBy(User::getSex)));
		System.out.println(u.stream().collect(Collectors
			.groupingBy(User::getSex,Collectors.groupingBy(it->{
				if(it.getAge()<=18) {
					return "未成年";
				}else {
					return "成年";
				}
			}))));
		System.out.println(u.stream().collect(Collectors.partitioningBy(it->it.getAge()<=18)));
	}
}
